package com.base;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author xingdongyang
 * @date 2020/7/17
 */
public class AtomicIntegerFieldUpdaterDemo {
    public static AtomicReference<Integer> atomicReference = new AtomicReference<>(0);

    public static void main(String[] args) throws InterruptedException {
        atomicReference.compareAndSet(0,2);
        atomicReference.compareAndSet(0,1);
        atomicReference.compareAndSet(1,3);
        atomicReference.compareAndSet(2,4);
        atomicReference.compareAndSet(3,5);
        System.out.println("atomicReference="+atomicReference);

        if(updater.compareAndSet(concurrencyTest,100,120)){
            System.out.println("update success "+concurrencyTest.getCount());
        }

        if(updater.compareAndSet(concurrencyTest,100,130)){
            System.out.println("update success "+concurrencyTest.getCount());
        } else {
            System.out.println("update fail "+concurrencyTest.getCount());
        }

    }


    public static AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterDemo> updater = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterDemo.class,"count");

    private volatile int count = 100;

    public int getCount(){
        return count;
    }

    private static AtomicIntegerFieldUpdaterDemo concurrencyTest = new AtomicIntegerFieldUpdaterDemo();
}